import config from '@/config';
import axios from 'axios'
import { setToken,getToken } from '@/utils/tool';
import router from '@/router'

const baseURL = process.env.NODE_ENV !== 'production' ? config.baseUrl.dev : config.baseUrl.pro;

class HttpRequest {
    constructor(baseUrl = baseURL){
        this.baseUrl = baseUrl;
        this.queue = {};
    }


    //配置config
    getInSideConfig(){
        const config = {
            baseURL:this.baseUrl,
            timeout:200000,
            headers:{
                "token": getToken()
                //"Authorization": getToken()
            }
        };
        return config;
    }

    //配置拦截
    interceptors(instance,url){ //instance是axios的实例, url是请求的地址

        const loading = null;

        //请求拦截
        instance.interceptors.request.use(config=>{
                if(!Object.keys(this.queue).length){
                    //添加全局的loading
                }
                this.queue[url] = true; // 标记: 确认
                return config;
            },
            error=>{
                return Promise.reject("请求前出现错误,错误是:"+error);
            });

        //响应拦截
        instance.interceptors.response.use(res=>{

                if(res.data.code == '101'){
                    alert("登陆超时，请重新登陆");
                    setToken('');
                    router.push({ name:'login' });
                    return false;
                }


                if(res.status == 500){
                    alert("您的网络不稳定, 请稍后再试...");
                    setToken('');
                    router.push({ name:'login' });
                    return false;
                }

                /*if(res.data.success == false){
                    alert(res.data.message);
                    setToken('');
                    router.push({ name:'login' });
                    return false;
                }*/
                delete this.queue[url];
                return res;
            },
            error=>{
                delete this.queue[url];
                return Promise.reject("请求后出现错误,错误是:"+error);
            });
    }

    //实例化axios
    request(options){
        const instance = axios.create();
        options = Object.assign(this.getInSideConfig(),options);//合并配置

        this.interceptors(instance,options.url);
        return instance(options);
    }
}

export default HttpRequest;

